<!--开头三个按钮的代码-->
    function preview(){
    let jiaoshou = jiezhenYisheng.value;
    if(jiaoshou==''){
    alert('接诊医生为空！无法生成病例！');
    return;
}
    let chufuzhen = encodeURIComponent(document.getElementById('jiuzhenleixin').value);
    let xingming = encodeURIComponent(document.getElementById('xingming').value);
    let nianling = encodeURIComponent(document.getElementById('nianling').value);
    let xingbie = encodeURIComponent(document.getElementById('xingbie').value);
    let hunkuang = encodeURIComponent(document.getElementById('hunkuang').value);
    let nian = encodeURIComponent(document.getElementById('year').value);
    let yue = encodeURIComponent(document.getElementById('month').value);
    let ri = encodeURIComponent(document.getElementById('day').value);
    let otherTime = encodeURIComponent(document.getElementById('otherTime').value);
    let detail = encodeURIComponent(document.getElementById('zhushu').value+'。'+document.getElementById('xianbingshi').value);
    let chufang;
    if(xiangban.style.display=='none'){  //简版
    chufang = encodeURIComponent(JSON.stringify(data_J));
}
    else {  //详细版本
    chufang = encodeURIComponent(JSON.stringify(data_X));
}
    var dataToSend = {
    jiaoshou: jiaoshou,
    xingming: xingming,
    nianling : nianling,
    xingbie : xingbie,
    hunkuang : hunkuang,
    time : nian + ' '+yue + ' '+ri+'' +otherTime,
    detail : detail,
    chufang :chufang,
    chufuzhen: chufuzhen
};
    var queryString = Object.keys(dataToSend).map(key => key + '=' + dataToSend[key]).join('&');
    var newPageUrl = '/wxya/blShow?' + queryString;
    window.open(newPageUrl, '_blank');
}
    function downWord(){
    let jiaoshou = jiezhenYisheng.value;
    if(jiaoshou==''){
    alert('接诊医生为空！无法生成病例！');
    return;
}
    let chufuzhen = document.getElementById('jiuzhenleixin').value;
    let xingming = document.getElementById('xingming').value;
    let nianling = document.getElementById('nianling').value;
    let xingbie = document.getElementById('xingbie').value;
    let hunkuang = document.getElementById('hunkuang').value;
    let nian = document.getElementById('year').value;
    let yue = document.getElementById('month').value;
    let ri =document.getElementById('day').value;
    let otherTime =document.getElementById('otherTime').value;
    let detail = document.getElementById('zhushu').value+'。'+document.getElementById('xianbingshi').value;
    let chufang , time = nian + ' '+yue + ' '+ri+'' +otherTime;
    if(xiangban.style.display=='none'){  //简版
    chufang = data_J;
}
    else {  //详细版本
    chufang = data_X;
}
    let fileName = jiaoshou + '教授的门诊案例——'+xingming;
    let hzData = xingming +' '+ xingbie + ' '+nianling + ' '+hunkuang;
    let tempChufang='';
    chufang.forEach(function (x){
    let zhongyao = x.zhongyao;
    let yaoliang = x.yaoliang;
    let paozhi = x.paozhi;
    let chandi = x.chandi;
    let beizhu = x.beizhu;
    let daiding = x.daiding;
    tempChufang+=zhongyao;
    if(yaoliang!=null){
    tempChufang+=' '+yaoliang;
}
    if(paozhi!=null){
    tempChufang+=' '+paozhi;
}
    if(chandi!=null){
    tempChufang+=' '+chandi;
}
    if(beizhu!=null){
    tempChufang+=' '+beizhu;
}
    if(daiding!=null){
    tempChufang+=' '+daiding;
}
    tempChufang+=' ；';
});
    let czTime = time;
    let fzTime = time;
    let czDetail = detail;
    let fzDetail = detail;
    let chufang_1 = tempChufang;
    let chufang_2 = tempChufang;

    if (chufuzhen === "初诊") {
    fzTime = "";
    fzDetail = "";
    chufang_2 = "";
    czDetail = czDetail === "。" ? "" : czDetail;
} else {
    czTime = "";
    czDetail = "";
    chufang_1 = "";
    fzDetail = fzDetail === "。" ? "" : fzDetail;
}
    const textContent = '病例信息\n备注：'+jiaoshou+'的门诊案例\n'+'患者信息：'+hzData+"\n初诊时间："+czTime+"\n初诊病史："+czDetail+"\n初诊用药："+chufang_1+"\n复诊时间："+fzTime+"\n复诊病史："+fzDetail+"\n复诊诊用药："+chufang_2;


    const blob = new Blob([textContent], { type: "application/msword" });

    // 创建下载链接
    const downloadLink = document.createElement("a");
    downloadLink.href = URL.createObjectURL(blob);
    downloadLink.download = fileName+".doc";
    downloadLink.textContent = "Download Word";
    // 模拟点击下载链接
    downloadLink.click();
    // 清理创建的链接
    URL.revokeObjectURL(downloadLink.href);
}
    function downPdf(){
    let jiaoshou = jiezhenYisheng.value;
    if(jiaoshou==''){
    alert('接诊医生为空！无法生成病例！');
    return;
}
    let chufuzhen = document.getElementById('jiuzhenleixin').value;
    let xingming = document.getElementById('xingming').value;
    let nianling = document.getElementById('nianling').value;
    let xingbie = document.getElementById('xingbie').value;
    let hunkuang = document.getElementById('hunkuang').value;
    let nian = document.getElementById('year').value;
    let yue = document.getElementById('month').value;
    let ri =document.getElementById('day').value;
    let otherTime =document.getElementById('otherTime').value;
    let detail = document.getElementById('zhushu').value+'。'+document.getElementById('xianbingshi').value;
    let chufang , time = nian + ' '+yue + ' '+ri+'' +otherTime;
    if(xiangban.style.display=='none'){  //简版
    chufang = data_J;
}
    else {  //详细版本
    chufang = data_X;
}
    let fileName = jiaoshou + '教授的门诊案例——'+xingming;
    let hzData = xingming +' '+ xingbie + ' '+nianling + ' '+hunkuang;
    let tempChufang='';
    chufang.forEach(function (x){
    let zhongyao = x.zhongyao;
    let yaoliang = x.yaoliang;
    let paozhi = x.paozhi;
    let chandi = x.chandi;
    let beizhu = x.beizhu;
    let daiding = x.daiding;
    tempChufang+=zhongyao;
    if(yaoliang!=null){
    tempChufang+=' '+yaoliang;
}
    if(paozhi!=null){
    tempChufang+=' '+paozhi;
}
    if(chandi!=null){
    tempChufang+=' '+chandi;
}
    if(beizhu!=null){
    tempChufang+=' '+beizhu;
}
    if(daiding!=null){
    tempChufang+=' '+daiding;
}
    tempChufang+=' ；';
});
    let czTime = time;
    let fzTime = time;
    let czDetail = detail;
    let fzDetail = detail;
    let chufang_1 = tempChufang;
    let chufang_2 = tempChufang;

    if (chufuzhen === "初诊") {
    fzTime = "";
    fzDetail = "";
    chufang_2 = "";
    czDetail = czDetail === "。" ? "" : czDetail;
} else {
    czTime = "";
    czDetail = "";
    chufang_1 = "";
    fzDetail = fzDetail === "。" ? "" : fzDetail;
}
    const textContent = '病例信息\n备注：'+jiaoshou+'的门诊案例\n'+'患者信息：'+hzData+"\n初诊时间："+czTime+"\n初诊病史："+czDetail+"\n初诊用药："+chufang_1+"\n复诊时间："+fzTime+"\n复诊病史："+fzDetail+"\n复诊诊用药："+chufang_2;
    var formData =new FormData();
    formData.append("content",textContent);
    formData.append("name",fileName);
    fetch('/wxya/generate-pdf', {
    method: 'POST',
    body: formData, // Ensure formData contains the necessary data

})
    .then(response => response.blob())
    .then(blob => {
    var url = URL.createObjectURL(blob);
    var a = document.createElement('a');
    a.href = url;
    a.download = fileName + ".pdf"; // Use the provided name for the filename
    a.click();
});
}
